<template>
  <div class="app-container">
    <el-card>
      <!--工具栏-->
      <div class="head-container">
        <eHeader :permission="permission"/>
        <crudOperation :permission="permission" />
      </div>
      <!--工具栏-->

      <!--表单组件-->
      <eForm  />

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="编号" />
        <el-table-column prop="name" label="名称"/>
        <el-table-column prop="classTime" label="上课时间"/>
        <el-table-column prop="classAddress" label="上课地点"/>
        <el-table-column prop="teacherName" label="教师"/>
        <el-table-column prop="type" label="课程类型">
          <template slot-scope="scope">
            <el-button v-if="scope.row.type == 1" type="primary" size="mini">必须</el-button>

            <el-button v-else type="primary" size="mini">选修</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="results" label="课程积分" />
        <el-table-column prop="weeks" label="周数" />
        <el-table-column prop="studentResults" label="学生分数" >

        </el-table-column>
        <el-table-column prop="studentName" label="学生姓名" />
        <el-table-column prop="addTime" label="创建时间" />
        <el-table-column  label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button @click="show(scope.row)" type="success" size="mini">打分</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </el-card>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="cancelCU"
      :visible="visible"
      title="打分"
      width="500px"
    >
      <el-form ref="form" :model="form" size="small" label-width="80px">

        <el-form-item
          label="学生分数"
          prop="results"
        >
          <el-input v-model="form.studentResults" style="width: 370px;"/>
        </el-form-item>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="text"
          @click="cancelCU"
        >
          取消
        </el-button>
        <el-button
          type="primary"
          @click="submitCU"
        >
          确认
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import crudStudentCourse from '@/api/ceamStudentCourse'
import { getCourseWithResults } from '@/api/ceamStudentCourse'
import { add } from '@/api/ceamResults'
import eForm from './form'
import eHeader from './header'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'

// crud交由presenter持有
const crud = CRUD({
  title: '成绩管理',
  url: 'api/studentCourse/cwr',
  sort: ['sort,asc', 'id,desc'],
  crudMethod: { ...crudStudentCourse }
})

export default {
  components: { eHeader, eForm, crudOperation, pagination, udOperation },
  mixins: [presenter(crud)],
  data() {
    return {
      loading: false,
      visible: false,
      form: {},
      data: [],
      delLoading: false,
      studentList: [],
      permission: {
        add: ['admin', 'results:add'],
        edit: ['admin', 'results:edit'],
        del: ['admin', 'results:del']
      }
    }
  },
  created() {
    this.$nextTick(() => {
      //this.init()
    })
    this.initData();
  },
  methods: {
    initData() {
      getCourseWithResults({page: 0, size: 10}).then(res => {
        this.data = res.records;
      })
    },
    cancelCU() {
      this.visible = false;
    },
    show(data) {
      this.form = data;
      this.visible = true;
    },
    submitCU() {
      this.visible = false;
      this.form.id = null;

      add(this.form).then(res => {
        //this.init();
        this.$notify({
          title: '成功',
          type: 'success',
          duration: 2500
        })
      }).catch(error => {
        this.$notify({
          title: '失败',
          type: 'danger',
          duration: 2500
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
